/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sesame.domain.Album;
import com.sesame.domain.AlbumTag;
import com.sesame.domain.ImageTag;
import com.sesame.service.AlbumService;

/**
 * @author voidman
 * 
 */
public class SetCoverController extends AbstractController {

    private AlbumService albumService;
    private Integer imageId = null;
    private Album album;

    public void setAlbumService(AlbumService albumService) {
        this.albumService = albumService;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        imageId = Integer.parseInt(request.getParameter("imageId"));
        String cover = albumService.showImage(imageId).getUrl();
        Integer albumId = albumService.showImage(imageId).getAlbumId();

        album.setId(albumId);
        album.setCover(cover);
        albumService.updateCover(album);

        return new ModelAndView("redirect:" + request.getHeader("referer"));

    }

}